import { directives } from '@/common/directives'

const runHook = (hookName, value, el, vnode, oldVnode) => {
  if (!value || !value.length) return
  for (const val of value) {
    const hook = (directives[val.name] || {})[hookName]
    hook && hook(el, val, vnode, oldVnode)
  }
}

/**
 * 暂时只支持全局指令
 */
export default {
  bind(el, binding, vnode) {
    runHook('bind', binding.value, el, vnode)
  },
  inserted(el, binding, vnode) {
    runHook('inserted', binding.value, el, vnode)
  },
  update(el, binding, vnode, oldVnode) {
    runHook('update', binding.value, el, vnode, oldVnode)
  },
  componentUpdated(el, binding, vnode, oldVnode) {
    runHook('componentUpdated', binding.value, el, vnode, oldVnode)
  },
  unbind(el, binding, vnode) {
    runHook('unbind', binding.value, el, vnode)
  },
}
